{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook illustrates the functions available in the textwrap module"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "    The textwrap module can be used to format text for output in\n",
      "    situations where pretty-printing is desired.  It offers\n",
      "    programmatic functionality similar to the paragraph wrapping\n",
      "    or filling features found in many text editors.\n",
      "    \n"
     ]
    }
   ],
   "source": [
    "import textwrap\n",
    "\n",
    "sample_text = '''\n",
    "    The textwrap module can be used to format text for output in\n",
    "    situations where pretty-printing is desired.  It offers\n",
    "    programmatic functionality similar to the paragraph wrapping\n",
    "    or filling features found in many text editors.\n",
    "    '''\n",
    "print(sample_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'     The textwrap module can be used to format text for output in     situations where pretty-\\nprinting is desired.  It offers     programmatic functionality similar to the paragraph wrapping\\nor filling features found in many text editors.'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "textwrap.fill(sample_text, width=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "The textwrap module can be used to format text for output in\n",
      "situations where pretty-printing is desired.  It offers\n",
      "programmatic functionality similar to the paragraph wrapping\n",
      "or filling features found in many text editors.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#removing existing indentation\n",
    "\n",
    "print(textwrap.dedent(sample_text))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50 Columns:\n",
      "\n",
      "The textwrap module can be used to format text for\n",
      "output in situations where pretty-printing is\n",
      "desired.  It offers programmatic functionality\n",
      "similar to the paragraph wrapping or filling\n",
      "features found in many text editors.\n",
      "\n",
      "100 Columns:\n",
      "\n",
      "The textwrap module can be used to format text for output in situations where pretty-printing is\n",
      "desired.  It offers programmatic functionality similar to the paragraph wrapping or filling features\n",
      "found in many text editors.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Combining dedent and fill\n",
    "\n",
    "dedented_text = textwrap.dedent(sample_text).strip()\n",
    "\n",
    "for width in [50, 100]:\n",
    "    print('{} Columns:\\n'.format(width))\n",
    "    print(textwrap.fill(dedented_text, width=width))\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "%%    The textwrap module can be used to format text for output in\n",
      "%%    situations where pretty-printing is desired.  It offers\n",
      "%%    programmatic functionality similar to the paragraph wrapping\n",
      "%%    or filling features found in many text editors.\n",
      "    \n",
      "\n",
      "\n",
      "=>    The textwrap module can be used to format text for output in\n",
      "=>    situations where pretty-printing is desired.  It offers\n",
      "=>    programmatic functionality similar to the paragraph wrapping\n",
      "=>    or filling features found in many text editors.\n",
      "    \n"
     ]
    }
   ],
   "source": [
    "#indenting blocks\n",
    "\n",
    "print(textwrap.indent(sample_text, '%%'))\n",
    "print()\n",
    "print(textwrap.indent(sample_text, '=>'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The textwrap module can be used to format text\n",
      "     for output in situations where pretty-\n",
      "     printing is desired.  It offers programmatic\n",
      "     functionality similar to the paragraph\n",
      "     wrapping or filling features found in many\n",
      "     text editors.\n"
     ]
    }
   ],
   "source": [
    "#Hanging indents - Controlling indents for first and subsequent lines differently\n",
    "\n",
    "print(textwrap.fill(dedented_text, initial_indent = ' ', subsequent_indent = ' ' * 5, width = 50))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The textwrap module can be used to format text for output in situations where pretty-printing [...]\n"
     ]
    }
   ],
   "source": [
    "#Truncating long texts\n",
    "\n",
    "print(textwrap.shorten(sample_text, 100))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
